package com.yict.common.util;

import java.util.Collection;

import org.springframework.util.Assert;

/**
 * <h3>Class name</h3> <h4>Description</h4>
 * 
 * <h4>Special Notes</h4>
 * 
 * 
 * @ver 0.1
 * @author Jay.Wu 2008-9-23
 * 
 */
public class AssertUtil {


	/**
	 * 抛出提示异常
	 * 
	 * @param object
	 * @param message
	 * @throws Exception
	 */
	public static void errorWithBundle(String message, Class<?> exception) {
		try {
			message = ResourceBundleUtil.getString(message);
		} catch (Exception e) {
		}

		// 使用指定构造函数
		throw (RuntimeException) ClassUtil.newObjectByConstructor(exception, new Class[] { String.class },
				new Object[] { message });
	}

	/**
	 * 判断是否为空
	 * 
	 * @param object
	 * @param message
	 * @throws Exception
	 */
	public static void notNullWithBundle(Object object, String message, Class<?> exception) {
		try {
			Assert.notNull(object, message);
		} catch (Exception e) {
			errorWithBundle(message, exception);
		}
	}

	/**
	 * 判断是否为空
	 * 
	 * @param object
	 * @param message
	 * @throws Exception
	 */
	public static void notEmptyWithBundle(Collection<?> collection, String message, Class<?> exception) {
		if (ListUtil.isEmpty(collection)) {
			errorWithBundle(message, exception);
		}

	}

	/**
	 * 判断是否唯一，否则抛出提示异常
	 * 
	 * @param object
	 * @param message
	 * @throws Exception
	 */
	public static void uniqueWithBundle(Collection<?> collection, String message, Class<?> exception) {
		if (ListUtil.isEmpty(collection)) {
			errorWithBundle(message + "_null", exception);
		}

		if (collection.size() != 1) {
			errorWithBundle(message + "_more", exception);
		}
	}

	/**
	 * 判断是否为真，若不为真，则抛出异常
	 * 
	 * @param b
	 * @param message
	 * @throws Exception
	 */
	public static void isTrueWithBundle(boolean b, String message, Class<?> exception) throws Exception {
		try {
			Assert.isTrue(b);
		} catch (Exception e) {
			errorWithBundle(message, exception);
		}
	}
}